/**
 * CicleDetailActivity.java
 * com.jamin.aabill.ui
 *
 * Function： TODO 
 *
 *   ver     date      		author
 * ──────────────────────────────────
 *   		 2012-8-28 		wangjieming
 *
 * Copyright (c) 2012, TNT All Rights Reserved.
 */

package com.jamin.aabill.ui;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.jamin.aabill.R;
import com.jamin.aabill.dao.CircleDao;
import com.jamin.aabill.data.Circle;
import com.jamin.aabill.utils.AppConstant;
import com.jamin.aabill.utils.TimeUtils;
import com.jamin.aabill.utils.Utils;

/**
 * ClassName:CicleDetailActivity
 *
 * @author   wangjieming
 * @version  1.0
 * @since    v1.0
 * @Date	 2012-8-28 上午9:01:37 
 */
public class CircleDetailActivity extends Activity{

	private static final String TAG = "CircleDetailActivity";

	BroadcastReceiver receiver = null;

	private TextView mMostCostTypeView = null;
	private TextView mDescriptionView = null;
	private TextView mCreateTimeView = null;
	private TextView mTotalCostView = null;


	private Circle circle = null;
	private long circleId = -1;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.circle_detail_activity);
		circleId = getIntent().getLongExtra("circleId", -1);
		Utils.log(TAG, "circleId = " + circleId);
		CircleDao mDao = new CircleDao();
		circle = mDao.getCircle(circleId);
		initView();
		refreshView();
		receiver = new RefreshBroadcastReceiver();
		registerReceiver(receiver, new IntentFilter(AppConstant.BILL_DETAIL_REFRESH_ACTION));

	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		unregisterReceiver(receiver);
		Utils.log(TAG, "CircleDetailActivity=========onDestroy");

	}



	private void initView() {

		mMostCostTypeView = (TextView) findViewById(R.id.circle_detail_most_cost_type_text);
		mDescriptionView = (TextView) findViewById(R.id.circle_detail_description_text);
		mCreateTimeView = (TextView) findViewById(R.id.circle_detail_create_time_text);
		mTotalCostView = (TextView) findViewById(R.id.circle_detail_total_text);
	}

	/**
	 * 在View再次可见的时候刷新页面数据<br>
	 * @method refreshView
	 * @throws 
	 * @since v1.0
	 */
	private void refreshView() {
		mDescriptionView.setText(getString(R.string.bill_detail_activity_descrption , circle.getCircle_description()));
		String createTime = AABillApplication.mApplicationContext.getString(R.string.bill_detail_activity_create_time, 
				TimeUtils.FormatTimeForm(circle.getCircle_create_time())) ;
		mCreateTimeView.setText(createTime);
		mTotalCostView.setText(getString(R.string.cirlce_detail_activity_total_cost, AABillApplication.mBillManager.getCircleTotalCost(circleId)));
	}





	class RefreshBroadcastReceiver extends BroadcastReceiver{

		@Override
		public void onReceive(Context ctx, Intent intent) {
			if(intent.getAction().equals(AppConstant.BILL_DETAIL_REFRESH_ACTION)){
				refreshView();
			}
		}

	}
}

